package com.han.agent.controller;

import com.han.agent.interview.InterviewService;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.evaluation.RelevancyEvaluator;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.document.Document;
import org.springframework.ai.evaluation.EvaluationRequest;
import org.springframework.ai.evaluation.EvaluationResponse;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/interview/assistant")
@RestController
public class InterviewAssistantController {

    private final OpenAiChatModel chatModel;
    private final ChatClient chatClient;
    private final InterviewService interviewService;

    public InterviewAssistantController(OpenAiChatModel chatModel, ChatClient.Builder chatClientBuilder, InterviewService interviewService) {
        this.chatModel = chatModel;
        this.chatClient = chatClientBuilder.build();
        this.interviewService = interviewService;
    }

    @GetMapping("/documentLoad")
    public List<Document> document() {
        return interviewService.loadText();
    }

    @GetMapping("/documentSearch")
    public String documentSearch() {
        return interviewService.search("如何理解Java中的装箱与拆箱").get(0).getText();
    }

    @GetMapping(value = "/documentSearch/flux", produces = "text/html;charset=UTF-8")
    public Flux<String> flux(@RequestParam String question) {
        // 向量搜索
        List<Document> documentList = interviewService.search(question);

        if (CollectionUtils.isEmpty(documentList)) {
            return Flux.just("未查询到数据");
        }

        // 提示词模板
        PromptTemplate promptTemplate = new PromptTemplate("{userMessage}\n\n 用中文，并根据以下信息回答问题，不要随意扩展:\n {contents}");

        // 组装提示词
        Prompt prompt = promptTemplate.create(Map.of("userMessage", question, "contents", documentList));

        // 调用大模型
        return chatClient.prompt(prompt).stream().content();
    }

    @GetMapping("/documentSearch/evaluation")
    public Map<String, Object> evaluation(@RequestParam String question) {
        // 向量搜索
        List<Document> documentList = interviewService.search(question);

        if (CollectionUtils.isEmpty(documentList)) {
            return Collections.singletonMap(question, "未查询到数据");
        }

        PromptTemplate promptTemplate = new PromptTemplate("{userMessage}\n\n 用中文，并根据以下信息回答问题，不要随意扩展:\n {contents}");

        Prompt prompt = promptTemplate.create(Map.of("userMessage", question, "contents", documentList));

        // 调用大模型
        String content = chatClient.prompt(prompt).call().content();

        // 评估是否产生了幻觉
        var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModel));
        EvaluationRequest evaluationRequest = new EvaluationRequest(question, documentList, content);
        EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("question", question);
        resultMap.put("content", content);
        resultMap.put("pass", evaluationResponse.isPass());
        resultMap.put("score", evaluationResponse.getScore());
        return resultMap;
    }


}
